package com.mba.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.mba.util.Utility;

/*
 * resource class of message
 * 
 * Written by pansen at 2013/04/17
 * 
 */

public class Message {

	/*
	 * ID of message
	 */
	private long messageID;

	/*
	 * content of message
	 */
	private String text;

	/*
	 * creatTime
	 */
	private Date createAt;

	/*
	 * location of message
	 */
	private String location;
	
	/*
	 * area of message
	 */
	private Area area;

	/*
	 * user that create message
	 */
	private UserBasic user;

	/*
	 * commentsCount and detail comments of message
	 */
	int commentsCount;
	List<Comment> comments;

	public Message(){}
	
	
	public Message(JSONObject jsonObj)throws Exception{
		constructJson(jsonObj);
	}
	
	public Message(long MessageID, String text, Date createAt,
			String location, Area area, UserBasic user, int commentsCount,
			List<Comment> comments) {
		this.messageID = MessageID;
		this.text = text;
		this.createAt = createAt;
		this.location = location;
		this.area = area;
		this.user = user;
		this.commentsCount = commentsCount;
		this.comments = comments;
	}
	
	private void constructJson(JSONObject json) throws Exception {
		try {
			createAt = Utility.parseDate(json.getString("created_at"), "yyyy-MM-dd HH:mm");
			messageID = Utility.getLong("message_id",json);
			commentsCount=Utility.getInt("comments_count",json);
			text = json.getString("text");
			if(!json.isNull("user")){
				user = new UserBasic(json.getJSONObject("user"));
			}
			if(!json.isNull("comments")){
				JSONArray list = json.getJSONArray("comments");
				int size = list.length();
				comments = new ArrayList<Comment>(size);
				for (int i = 0;i< size;i++){
					comments.add(new Comment(list.getJSONObject(i)));
				}
			}
			location = json.getString("location");
			commentsCount = json.getInt("comments_count");
			
		} catch (JSONException je) {
			throw je;
		}
	}

	public long getMessageID() {
		return messageID;
	}

	public void setMessageID(long messageID) {
		this.messageID = messageID;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public Date getCreateAt() {
		return createAt;
	}

	public void setCreateAt(Date createAt) {
		this.createAt = createAt;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public UserBasic getUser() {
		return user;
	}

	public void setUser(UserBasic user) {
		this.user = user;
	}

	public int getCommentsCount() {
		return commentsCount;
	}

	public void setCommentsCount(int commentsCount) {
		this.commentsCount = commentsCount;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public Area getArea() {
		return area;
	}

	public void setArea(Area area) {
		this.area = area;
	}
}
